/** 禁止滚动穿透方法 */
export const preventScroll = function () {
  // #ifdef MP-WEIXIN
  wx.setPageStyle({
    style: {
      overflow: 'hidden'
    }
  });
  // #endif
  // #ifdef MP-XHS
  xhs?.setBodyStyle({
    style: 'overflow: hidden;'
  });
  // #endif
  // #ifdef H5
  document.body.style.overflow = 'hidden';
  // #endif
};

/** 恢复滚动穿透方法 */
export const restoreScroll = function () {
  // #ifdef MP-WEIXIN
  wx.setPageStyle({
    style: {
      overflow: ''
    }
  });
  // #endif
  // #ifdef MP-XHS
  xhs?.setBodyStyle({
    style: ''
  });
  // #endif
  // #ifdef H5
  document.body.style.overflow = '';
  // #endif
};

export const jump = (url: string) => {
  uni.navigateTo({
    url,
    fail() {
      uni.switchTab({
        url
      });
    }
  });
};

export const isString = (value: any): boolean => {
  return Object.prototype.toString.call(value) === '[object String]';
};
export const isNumber = (value: any): boolean => {
  return Object.prototype.toString.call(value) === '[object Number]';
};

export const checkUpdate = () => {
  try {
    const updateManager = uni.getUpdateManager();
    updateManager.onCheckForUpdate((res) => {
      if (res.hasUpdate) {
        updateManager.onUpdateReady(() => {
          uni.showModal({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            success: (result) => {
              if (result.confirm) {
                updateManager.applyUpdate();
              }
            }
          });
        });
      }
    });
  } catch (e) {
    //TODO handle the exception
    console.log(e);
  }
};
